package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.PrPrepaymentFeeReq;
import com.jrx.anytxn.param.dto.product.PrPrepaymentFeeRes;
import com.jrx.anytxn.param.dto.product.PrPreterminationFeeTableReq;
import com.jrx.anytxn.param.dto.product.PrPreterminationFeeTableRes;
import com.jrx.anytxn.param.entity.PrPrepaymentFee;
import com.jrx.anytxn.param.entity.PrPreterminationFeeTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrPrepaymentFeeMapper;
import com.jrx.anytxn.param.mapper.ext.ExtPrPreterminationFeeTableMapper;
import com.jrx.anytxn.param.service.product.IPrPrepaymentFeeService;
import com.jrx.anytxn.param.service.product.IPrPreterminationFeeTableService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 提前还款手续费表 业务接口实现
 *
 * @author yx
 * @date 2019/5/28
 */
@Service
@CacheConfig(cacheNames = "PREPAYMENT_FEE")
public class PrPrepaymentFeeServiceImpl implements IPrPrepaymentFeeService {
    private static final Logger logger = LoggerFactory.getLogger(PrPrepaymentFeeServiceImpl.class);
    @Resource
    private ExtPrPrepaymentFeeMapper extPrPrepaymentFeeMapper;

    @Autowired
    private ISegmentService segmentService;


    @Override
    public TxnPage<PrPrepaymentFeeRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrPrepaymentFee> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrPrepaymentFee> prPrepaymentFeeList = extPrPrepaymentFeeMapper.selectByTenId(tenantId);
        if (prPrepaymentFeeList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrPrepaymentFeeRes> res = BeanMapping.copyList(prPrepaymentFeeList, PrPrepaymentFeeRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrPrepaymentFeeRes addPrPrepaymentFee(PrPrepaymentFeeReq prPrepaymentFeeReq) throws TxnException {
        PrPrepaymentFee prPrepaymentFee = extPrPrepaymentFeeMapper.selectByPrepaymentIdAndOrganizationId(prPrepaymentFeeReq.getPrepaymentFeeId(), prPrepaymentFeeReq.getOrganizationId(), prPrepaymentFeeReq.getTenantId());
        if (prPrepaymentFee != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrPrepaymentFee prepaymentFee = BeanMapping.copy(prPrepaymentFeeReq, PrPrepaymentFee.class);
        try {
            prepaymentFee.setId(segmentService.getId(Constant.PR_PREPAYMENT_FEE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        prepaymentFee.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrPrepaymentFeeMapper.insertSelective(prepaymentFee);
        return BeanMapping.copy(prepaymentFee, PrPrepaymentFeeRes.class);
    }

    @CacheEvict(key = "'prPrepaymentFee_'+#prPrepaymentFeeReq.tenantId+'_'+#prPrepaymentFeeReq.prepaymentFeeId")
    @Override
    public PrPrepaymentFeeRes modifyPrPrepaymentFee(PrPrepaymentFeeReq prPrepaymentFeeReq) throws TxnException {
        PrPrepaymentFee prPrepaymentFee = extPrPrepaymentFeeMapper.selectByTenIdAndFeeId(prPrepaymentFeeReq.getTenantId(),prPrepaymentFeeReq.getPrepaymentFeeId());
        if (prPrepaymentFee == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrPrepaymentFee prepaymentFee = BeanMapping.copy(prPrepaymentFeeReq, PrPrepaymentFee.class);
        // 更新
        extPrPrepaymentFeeMapper.updateByTenIdAndFeeId(prepaymentFee);
        return BeanMapping.copy(prepaymentFee, PrPrepaymentFeeRes.class);

    }

    @CacheEvict(key = "'prPrepaymentFee_'+#tenantId+'_'+#feeId")
    @Override
    public Boolean removeByTenIdAndFeeId(String tenantId, String feeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(feeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrPrepaymentFeeMapper.deleteByTenIdAndFeeId(tenantId, feeId);
        return i > 0;
    }

    @Override
    public PrPrepaymentFeeRes findByTenIdAndFeeId(String tenantId, String feeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(feeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrPrepaymentFee prepaymentFee = extPrPrepaymentFeeMapper.selectByTenIdAndFeeId(tenantId, feeId);
        return BeanMapping.copy(prepaymentFee, PrPrepaymentFeeRes.class);
    }

    @Cacheable(key = "'prPrepaymentFee_'+#tenantId+'_'+#prepaymentFeeId")
    @Override
    public PrPrepaymentFeeRes findByPrepaymentFeeId(String prepaymentFeeId, String orgId, String tenantId) throws TxnBizException {
        PrPrepaymentFee prepaymentFee = extPrPrepaymentFeeMapper.selectByPrepaymentIdAndOrganizationId(prepaymentFeeId, orgId, tenantId);
        if (prepaymentFee == null) {
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg(ErrorMsgConstant.PREPAYMENT_FEE));
        }
        return BeanMapping.copy(prepaymentFee, PrPrepaymentFeeRes.class);
    }
}
